package com.acme.bankapp.domain.bank;
import java.util.ArrayList;

import com.acme.bankapp.domain.bank.exceptions.ClientExistsException;


/**
 * Class Bank contains aggregate of clients 
 * @author Vasilyev Anton
 *
 */
public class Bank {
	
	private ArrayList<Client> clients;
	private ClientRegistrationListener[] listeners;

	public Bank(ArrayList<Client> clients,
			ClientRegistrationListener[] listeners) {
		this.listeners = listeners;
		this.clients = clients;
	}
	
	public Bank(ClientRegistrationListener[] listeners) {
		this(new ArrayList<Client>(), listeners);
	}

	public Bank(ArrayList<Client> clients) {
		this();
		this.clients.addAll(clients);
	}
	
	public Bank() {
		this.clients = new ArrayList<Client>();
		this.listeners = new ClientRegistrationListener[2];
		this.listeners[0] = new EmailNotificationListener();
		this.listeners[1] = new PrintClientListener();
	}

	public ArrayList<Client> getClients() {
		return clients;
	}
	
	public void addClient(Client client) throws ClientExistsException {
		if (findClient(client) != null) {
			throw new ClientExistsException();
		} else {
		
			for (ClientRegistrationListener listener : this.listeners) {
				listener.onClientAdded(client);
			}
			clients.add(client);
		}
	}
	
	public Client findClient(Client client) {
		for (Client client2: this.clients) {
			if (client2.equals(client))
				return client2;
		}
		return null;
	}

	public Client findClient(Gender gender, String name) {
		return this.findClient(new Client(gender, name));
	}

	
	public class EmailNotificationListener implements ClientRegistrationListener {
		@Override
		public void onClientAdded(Client c) {
			System.out.println("Notification email for client "
					+ c.getClientSalutation() + " to be sent");
		}
	}

	public class PrintClientListener implements ClientRegistrationListener {
		@Override
		public void onClientAdded(Client c) {
			System.out.println(c.getClientSalutation());
		}
	}
	
}
